package com.open4cn.simulator.config.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.open4cn.simulator.config.entity.DeviceEntity;
import com.open4cn.simulator.config.server.IDeviceServer;
import com.open4cn.simulator.core.device.DeviceParameters;
import com.open4cn.simulator.core.exception.IllegalParameterException;
import com.open4cn.simulator.device.DeviceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-04 04:02
 */
@RestController
@RequestMapping("manager/device")
public class DeviceManagerController {

    @Autowired
    private IDeviceServer deviceServer;

    @GetMapping("/load/{id}")
    public Object load(@PathVariable(name = "id") String id) {
        if (CharSequenceUtil.isBlank(id)) {
            throw new IllegalParameterException();
        }
        DeviceEntity deviceEntity = deviceServer.get(id);
        if (null == deviceEntity) {
            throw new IllegalParameterException("设备不存在");
        }

        DeviceParameters deviceParameters = new DeviceParameters();
        deviceParameters.setId(deviceEntity.getId());
        deviceParameters.setName(deviceEntity.getName());
        deviceParameters.setBrand(deviceEntity.getBrand());
        deviceParameters.setModel(deviceEntity.getModel());
        Map<String, Object> toMap = BeanUtil.beanToMap(deviceEntity);
        if (CharSequenceUtil.isNotBlank(deviceEntity.getConfig())) {
            String config = deviceEntity.getConfig();
            JSON json = JSONUtil.parse(config);
            Map<String, Object> map = json.toBean(Map.class);
            toMap.putAll(map);
        }
        toMap.forEach(deviceParameters::appendOrCover);

        DeviceManager deviceManager = DeviceManager.getInstance();
        deviceManager.load(deviceParameters);
        return deviceManager.snapshot(deviceParameters.getId());
    }

    @GetMapping("/start/{id}")
    public Object start(@PathVariable(name = "id") String id) {
        if (CharSequenceUtil.isBlank(id)) {
            throw new IllegalParameterException();
        }
        DeviceManager deviceManager = DeviceManager.getInstance();
        deviceManager.start(id);
        return deviceManager.snapshot(id);
    }

    @GetMapping("/get/{id}")
    public Object get(@PathVariable(name = "id") String id) {
        if (CharSequenceUtil.isBlank(id)) {
            throw new IllegalParameterException();
        }
        DeviceManager deviceManager = DeviceManager.getInstance();
        return deviceManager.get(id);
    }

    @GetMapping("/stop/{id}")
    public Object stop(@PathVariable(name = "id") String id) {
        if (CharSequenceUtil.isBlank(id)) {
            throw new IllegalParameterException();
        }
        DeviceManager deviceManager = DeviceManager.getInstance();
        return deviceManager.stop(id);
    }


    @GetMapping("/suspend/{id}")
    public Object suspend(@PathVariable(name = "id") String id) {
        if (CharSequenceUtil.isBlank(id)) {
            throw new IllegalParameterException();
        }
        DeviceManager deviceManager = DeviceManager.getInstance();
        deviceManager.suspend(id);
        return deviceManager.snapshot(id);
    }


    @GetMapping("/snapshot")
    public Object snapshot() {
        DeviceManager deviceManager = DeviceManager.getInstance();
        return deviceManager.snapshot();
    }

}
